-----------------------------------------------
-- 跨服操作项

-- human.db.middleOptions	跨服操作列表
-- addItem 					添加道具
-- updateYuanbao 			刷新水晶
-- updateYinliang 			刷新银两
------------------------------------------------

local Log = require("common.Log")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local MailLogic = require("mail.MailLogic")

-- 操作类型枚举
OPTYPE_ADD_ITEM = 1 		-- 背包添加道具
OPTYPE_UPDATE_YUANBAO = 2 	-- 添加/扣水晶
OPTYPE_UPDATE_YINLIANG = 3 	-- 添加/扣金币

-- 操作白名单，预防用
LOGTYPE_WHITE_LIST = {
	["minewar"] = true, -- reyes todo 这里要改成战火大陆的logtype
}

cmd = cmd or {}

local function getOptionsByType(human, optype)
	human.db.middleOptions = human.db.middleOptions or {}
	human.db.middleOptions[optype] = human.db.middleOptions[optype] or {}
	return human.db.middleOptions[optype]
end

local function checkLogType(logType, optype)
	if LOGTYPE_WHITE_LIST[logType] then
		return true
	end	
	Log.write(Log.LOGID_TEST, "not in white list", logType, optype)
end

function getItemCntByID(human, itemID, logType)
	if not _G.is_middle then return end
	if not checkLogType(logType, OPTYPE_ADD_ITEM) then return end
	local options = getOptionsByType(human, OPTYPE_ADD_ITEM)
	if options[itemID] == nil then return end
	return options[itemID][logType]
end

-- 跨服添加道具
function addItem(human, itemID, itemCnt, logType)
	if not _G.is_middle then return end
	if not checkLogType(logType, OPTYPE_ADD_ITEM) then return end
	local options = getOptionsByType(human, OPTYPE_ADD_ITEM)
	options[itemID] = options[itemID] or {}
	options[itemID][logType] = (options[itemID][logType] or 0) + itemCnt
end

cmd[OPTYPE_ADD_ITEM] = function(human, options)
	local failList = nil
	for itemID, option in pairs(options) do
		for logType, itemCnt in pairs(option) do
			if not BagLogic.addItem(human, itemID, itemCnt, nil, logType) then
				if logType == "minewar" then
					failList = failList or {}
					failList[#failList + 1] = {itemID, itemCnt}
				end
			end
		end
	end
	if failList then
		MailLogic.addMail(human,Lang.MINEWAR_MAIL_TITLE2,Lang.MINEWAR_MAIL_CONTENT2,failList)
	end
end


-- 跨服更新水晶
function updateYuanbao(human, value, logType)
	if not _G.is_middle then return end	
	if not checkLogType(logType, OPTYPE_UPDATE_YUANBAO) then return end
	local options = getOptionsByType(human, OPTYPE_UPDATE_YUANBAO)
	options[logType] = (options[logType] or 0) + value
end

cmd[OPTYPE_UPDATE_YUANBAO] = function(human, options)
	for logType, value in pairs(options) do
		if value > 0 then
			ObjHuman.addYuanbao(human, value, logType) 
		elseif value < 0 then
			ObjHuman.decYuanbao(human, "yuanbao", value, logType) 
		end
	end
end

-- 跨服更新金币
function updateYinliang(human, value, logType)
	if not _G.is_middle then return end	
	if not checkLogType(logType, OPTYPE_UPDATE_YINLIANG) then return end
	local options = getOptionsByType(human, OPTYPE_UPDATE_YINLIANG)
	options[logType] = (options[logType] or 0) + value	
end

cmd[OPTYPE_UPDATE_YINLIANG] = function(human, options)
	for logType, value in pairs(options) do
		if value ~= 0 then
			ObjHuman.updateYinliang(human, value, logType)
		end
	end
end

-- 回到本服需要处理跨服操作事件
function handleOptions(human, middleOptions)
	if _G.is_middle then return end	
	if not middleOptions then return end
	for optype, options in pairs(middleOptions) do
		if cmd[optype] then
			cmd[optype](human, options)
		end
	end
end


	